package com.qf.homework.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

//有个特殊的接口
public class ProductInterceptor implements HandlerInterceptor {
    /**
     * 注意，返回值 如果为 true，代表当前请求可以继续向后发送给 Controller中的handler方法
     * 如果返回false，那么当前的请求到此为止
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

       /*  //1. 校验验证码是否正确，从session中获取验证码和请求参数中的验证码进行比对
        // final String sessionCapchar = (String) session.getAttribute("captcha");
        //括号中的"captcha"去web.xml中     <param-value>captcha</param-value> 中 拿
       final HashMap<String, String> map = new HashMap<>();
        //获取前端向后端发送的数据
        String value = request.getParameter("value");
        String  result=null;
        if (sessionCapchar.equals(value)){
            result="1";
            response.getWriter().print(result);
            return true;
        }
        result="2";

        response.getWriter().print(result); */
        return false;

    }



    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("post~~~~~~");
        //获取上面的共享时间
        long start = (long) request.getAttribute("start");
        //结束时间戳
        long end = System.currentTimeMillis();
        //结束时间减去开始时间  就是共执行了多久
        System.out.println(handler+"执行时长："+(end-start));

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion~~~~~~");
    }
}
